#!/bin/sh
#
# Perform necessary datadog-iot-agent setup steps after package is installed.
# NOTE: for .rpm, see posttrans instead
#
# .deb: STEP 5 of 5
# .rpm: STEP 3 of 6

INSTALL_DIR=/opt/datadog-agent
LOG_DIR=/var/log/datadog
CONFIG_DIR=/etc/datadog-agent
SERVICE_NAME=datadog-agent

KNOWN_DISTRIBUTION="(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon|Arista|SUSE)"
DISTRIBUTION=$(lsb_release -d 2>/dev/null | grep -Eo $KNOWN_DISTRIBUTION || grep -Eo $KNOWN_DISTRIBUTION /etc/issue 2>/dev/null || grep -Eo $KNOWN_DISTRIBUTION /etc/Eos-release 2>/dev/null || grep -m1 -Eo $KNOWN_DISTRIBUTION /etc/os-release 2>/dev/null || uname -s)

# If we are inside the Docker container, do nothing
if [ -n "$DOCKER_DD_AGENT" ]; then
    echo "Installation from docker-dd-agent, nothing to do in postinst"
    exit 0
fi

# Linux installation
if [ "$DISTRIBUTION" != "Darwin" ]; then
    if [ -f "/etc/debian_version" ] || [ "$DISTRIBUTION" = "Debian" ] || [ "$DISTRIBUTION" = "Ubuntu" ]; then
        DISTRIBUTION_FAMILY="Debian"
    fi

    if [ "$DISTRIBUTION_FAMILY" = "Debian" ]; then
        set -e
        case "$1" in
            configure)
                # Only create dd-agent group and/or user if they don't already exist
                getent group dd-agent >/dev/null || (echo "Creating dd-agent group" && addgroup --system dd-agent --quiet)
                set +e
                id -u dd-agent >/dev/null 2>&1
                USER_EXISTS=$?
                set -e
                if [ ! $USER_EXISTS -eq 0 ]; then
                    echo "Creating dd-agent user"
                    adduser --system dd-agent --disabled-login --shell /usr/sbin/nologin --home ${INSTALL_DIR} --no-create-home --group --quiet
                elif id -nG dd-agent | grep --invert-match --word-regexp --quiet 'dd-agent'; then
                    # User exists but is not part of the dd-agent group
                    echo "Adding dd-agent user to dd-agent group"
                    usermod -g dd-agent dd-agent
                fi

                # Create a symlink to the agent's binary
                ln -sf $INSTALL_DIR/bin/agent/agent /usr/bin/datadog-agent
            ;;
            abort-upgrade|abort-remove|abort-deconfigure)
            ;;

            *)
            ;;
        esac
        #DEBHELPER#
    fi

    # Set proper rights to the dd-agent user
    chown -R dd-agent:dd-agent ${CONFIG_DIR}
    chown -R dd-agent:dd-agent ${LOG_DIR}
    chown -R dd-agent:dd-agent ${INSTALL_DIR}

    # Enable and restart the agent service here on Debian platforms
    # On RHEL, this is done in the posttrans script
    if [ "$DISTRIBUTION_FAMILY" = "Debian" ]; then
        # Only supports systemd and upstart
        echo "Enabling service $SERVICE_NAME"
        if command -v systemctl >/dev/null 2>&1; then
            systemctl enable $SERVICE_NAME || echo "[ WARNING ]\tCannot enable $SERVICE_NAME with systemctl"
        elif command -v initctl >/dev/null 2>&1; then
            # Nothing to do, this is defined directly in the upstart job file
            :
        else
            echo "[ WARNING ]\tCannot detect a supported init system. The datadog-iot-agent package only provides service files for systemd and upstart."
        fi

        # TODO: Use a configcheck command on the agent to determine if it's safe to restart,
        # and avoid restarting when a check conf is invalid
        if [ -f "$CONFIG_DIR/datadog.yaml" ]; then
            echo "(Re)starting $SERVICE_NAME now..."
            if command -v systemctl >/dev/null 2>&1; then
                systemctl restart $SERVICE_NAME || true
            elif command -v initctl >/dev/null 2>&1; then
                initctl start $SERVICE_NAME || initctl restart $SERVICE_NAME || true
            else
                echo "[ WARNING ]\tCannot detect a supported init system. The datadog-iot-agent package only provides service files for systemd and upstart."
            fi
        else
            # No datadog.yaml file is present. This is probably a clean install made with the
            # step-by-step instructions/an automation tool, and the config file will be added next.
            echo "No datadog.yaml file detected, not starting the agent"
        fi
    fi
else
	echo "The IoT Agent is not supported on MacOS"
	exit 0
fi

exit 0
